mui.init();
//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
//公共变量
var leaberid; //加入购物车或立即购买变量
//获得slider插件对象
var gallery = mui('.mui-slider');
gallery.slider({
	interval: 2000 //自动轮播周期，若为0则不自动播放，默认为0；
});

//店铺
$(document).on('tap', ".index", function() {
	var merchant_id = $(this).attr('merchant_id'); //商家id
	mui.openWindow({
		url: "shop_details.html",
		extras: {
			merchant_id: merchant_id
		},

	})
});

//客服
document.getElementById('service').addEventListener('tap', function() {
	mui.openWindow({
		url: "../my/my_online_service.html",
		id: "my_online_service",
	})
});

//购物车
document.getElementById('shopping').addEventListener('tap', function() {
	window.top.location.href = '../Shopping_Cart/Shopping_Cart.html';
});

//打开选择分类页面
document.getElementById('join_cart_btn').addEventListener('tap', function() {
	leaberid = $(this).attr('leaberid'); //购物车
	$(".mui-backdrop").css("display", "block");
	$('.mui-content').css('overflow', 'hidden');
});
document.getElementById('buy_cart_btn').addEventListener('tap', function() {
	leaberid = $(this).attr('leaberid'); //立即购买
	$(".mui-backdrop").css("display", "block");
	$(".mui-content").css('overflow', 'hidden');
});
//打开选择分类页面
document.getElementById('close_icon').addEventListener('tap', function() {
	$(".mui-backdrop").css("display", "none")
	$(".mui-content").css('overflow', 'visible');
});

//点击li时变颜色
mui(".changecolor").on('touchstart', 'span', function() {
	this.style.color = '#e93b3d'
});
mui(".changecolor").on('touchend', 'span', function() {
	this.style.color = '#a6a6a6'
});

//关闭当前页面，返回上一个页面
mui('body').on('tap', '.return_back', function() {
	mui.back();
});

var id; //商品id

//查询商品详情
function itemDetails() {
	mui.ajax(goods_url + '/goods/itemDetails', {
		data: {
			"id": id,
			"sign": "string"
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			//console.log(JSON.stringify(data));
			if(data.code == 10000) {
				var merchant_id = data.resp.user_id; //商家ID
				$(".index").attr('merchant_id', merchant_id); //给商家id赋值
				$("#img").val(data.resp.image); //商品图片
				$("#commercial").val(data.resp.business_name); //商家
				$("#url").val(data.resp.url); //图片地址
				//点击购物车绑定数据
				$("#shopping_title").text(data.resp.title); //商品名称
				$("#shopping_price").text(data.resp.price); //商品价格
				var detail_diagram = data.resp.details; //商品详情图
				var image_url = data.resp.url; //图片地址
				var detail_img = detail_diagram; //商品详情图
				$("#details_img").html(detail_img);
				var abbreviation_diagram = data.resp.image; //缩约图
				var abbreviation_diagram2 = image_url + abbreviation_diagram; //缩约图
				$("#abbreviation_diagram").attr('src', abbreviation_diagram2)
				var Sowing_map = data.resp.item_desc; //轮播图
				if(Sowing_map != null) {
					var sowing_map = Sowing_map.split(","); //截取字符串变成数组
				}
				//轮播图绑定
				var imgss = '../../images/detais_img.png';
				var img_html = '';
				var img_html2 = '<div class="mui-indicator mui-active" style="width:15px;height:4px;"></div>';
				if(Sowing_map == null) {
					$("#last_img").attr('src', imgss);
					$("#frist_img").attr('src', imgss);
					img_html += '<div class="mui-slider-item">';
					img_html += '<a href="#"><img src="../../images/detais_img.png"/></a>';
					img_html += '</div>';
					img_html2 += '<div class="mui-indicator" style="width:15px;height:4px;"></div>';
				} else {
					for(var i = 0; i < sowing_map.length; i++) {
						$("#last_img").attr('src', image_url + sowing_map[i]);
						$("#frist_img").attr('src', image_url + sowing_map[0]);
						img_html += '<div class="mui-slider-item">';
						img_html += '<a href="#"><img src=' + image_url + sowing_map[i] + '></a>';
						img_html += '</div>';
					}
					for(var i = 1; i < sowing_map.length; i++) {
						img_html2 += '<div class="mui-indicator" style="width:15px;height:4px;"></div>';
					}
				}
				$("#frist_img_box").before(img_html);
				$("#index_dian").html(img_html2);
				var gallery = mui('.mui-slider');
				gallery.slider({
					interval: 2000 //自动轮播周期，若为0则不自动播放，默认为0；
				});
				var html = "";
				html += '<p class="antistop_text">' + data.resp.title + '</p>';
				html += '<div class="price_delet_box mui-clearfix">';
				html += '<p class="discount_price mui-pull-left">￥<span>' + data.resp.price + '+';
				if(data.resp.special_of_integral == null) {
					html += '0权益积分';
				} else {
					html += data.resp.special_of_integral + '权益积分';
				}
				html += '</p>';
				if(data.resp.sales_volume == null) {
					html += '<p class="mui-pull-right">销量&nbsp;0</p>';
				} else {
					html += '<p class="mui-pull-right">销量&nbsp;' + data.resp.sales_volume + '</p>';
				}
				html += '</div>';
				html += '<div class="oneself">';
				if(data.resp.business_name == null) {
					html += '<p class="oneself_icon mui-pull-right">平台自营</p>';
				} else {
					html += '<p class="oneself_icon mui-pull-right">' + data.resp.business_name + '</p>';
				}
				html += '</div>';
			
				$('#slideshow_list').append(html);

				/*规格*/
				//console.log(JSON.stringify(data.resp.ext2));
				var ext2 = data.resp.ext2;
				var json = JSON.parse(ext2);
				if(json.size != "") {
					var size = json.size.split(",");
					console.log(size.length);
					var size_html = "<p>尺码</p><span class='choose_color'>" + size[0] + "</span>&nbsp;"; //颜色
					for(var i = 1; i < size.length; i++) {
						//如果有空的不做显示
						size_html += "<span>" + size[i] + "</span>&nbsp;";
					}
					$(".size").html(size_html);
				}

				if(json.spec.bool == "success") { //11/21
					if(json.spec.resp != "") {
						var spec = json.spec.resp;
						//console.log(JSON.stringify(spec));
						var spec_html = "<p>规格</p>" +
							"<p><span sta=" + spec[0].price + " class='choose_color'>" + spec[0].spec + "</span></p>"; //尺寸
						for(var i = 1; i < spec.length; i++) {
							spec_html += "<p><span sta=" + spec[i].price + ">" + spec[i].spec + "</span></p>";
						}
						$(".spec").html(spec_html);
						var sta = $(".spec .choose_color").attr("sta"); //
						$("#shopping_price").text(sta); //商品价格 
					}
				}
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});

}

//选择颜色
$(document).on('tap', ".spec span", function() {
	$(this).addClass("choose_color").parent().siblings().children().removeClass("choose_color");
	var sta = $(this).attr("sta");
	$("#shopping_price").text(sta); //商品价格 
});

//选择尺寸
$(document).on('tap', ".size span", function() {
	$(this).addClass("choose_color").siblings().removeClass("choose_color");
});

//加入购物车及立马购买
$(document).on('tap', '#join_cart_surebtn', function() {
	var spec = $(".spec .choose_color").text(); //尺寸
	var size = $(".size .choose_color").text(); //颜色 
	var price = $("#shopping_price").text(); //单价价格  
	if(user_id == null) {
		mui.openWindow({
			url: '../user/login.html',
			createNew: true
		})
		return false;
	}
	if(leaberid == 1) { //添加到购物车
		var item_number = $("#item_number").val(); //获取购买
		mui.ajax(goods_url + '/goods/addZshoppingTrolley', {
			data: {
				"item_id": id,
				"item_number": item_number,
				"sign": "string",
				"user_id": user_id,
				"price": price,
				"size": size,
				"spec": spec
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json'
			},
			success: function(data) {
				if(data.code == 10000) {
					mui.toast("添加成功", {
						duration: 'short',
						type: 'div' /* 是否使用h5绘制的对话框 */
					})
					$(".mui-backdrop").css("display", "none")
					$(".mui-content").css('overflow', 'visible');
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理；
				console.log(type);
				var error_msg = JSON.parse(xhr.responseText)
				mui.alert(error_msg.msg)
			}
		});
	}
	if(leaberid == 2) {
		var item_pricing = $("#shopping_price").text(); //商品单价
		var single_number = $("#item_number").val(); //下单数量
		var total_commodity_price = Number(item_pricing) * Number(single_number); //商品总价
		var image = $("#img").val(); //商品图片
		var title = $(".antistop_text").text(); //商品名称 
		var commercial = $("#commercial").val(); //商家
		var url = $("#url").val(); //图片地址
		mui.openWindow({
			url: '../Shopping_Cart/Order_information.html',
			id: 'Order_information',
			extras: {
				leaberid: 2,
				price: item_pricing,
				item_number: single_number,
				goodsid: id,
				Total_price: total_commodity_price,
				img: image,
				name: title,
				business: commercial,
				imgurl: url,
				specs: spec,
				sizes: size
			},
			createNew: true
		})
	}
})

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		id = self.commodity_id;
		itemDetails();
	})
}